// pages/luxian/index.js
let QQMapWX = require('../../lib/qqmap-wx-jssdk.js');
var qqmapsdk;
let qqMap = new QQMapWX({
  key: '64CBZ-OA5K4-KH3UV-DMCQX-2P4MJ-VJFQD' // 必填
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city:''
  },

  // 地图导航
  goGPS(e) {
    var that=this;
    var form=e.detail.value;
    var saddress=form.saddress;
    var eaddress=form.eaddress;
    qqMap.geocoder({
      address: that.data.city+saddress,//拼接一下城市，但是其实没必要。。
      complete: res => {
        console.log("开始经纬度："+JSON.stringify(res.result.location)); //经纬度对象
        var slat=res.result.location.lat;
        var slng=res.result.location.lng
        qqMap.geocoder({
          address: that.data.city+eaddress,
          complete: re => {
            console.log("终点经纬度："+re.result.location); //经纬度对象
            var elat=re.result.location.lat;
            var elng=re.result.location.lng
            let startPoint = JSON.stringify({ //终点
              'name': saddress,
              'latitude': slat,
              'longitude': slng
            });
             let endPoint = JSON.stringify({ //终点
               'name': eaddress,
               'latitude': elat,
               'longitude': elng
             });
             var key="64CBZ-OA5K4-KH3UV-DMCQX-2P4MJ-VJFQD";
             let referer = '地摊';
             wx.navigateTo({
               url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint + '&startPoint=' + startPoint
             }); 

          }
        });

      }
    });
    return;
    qqMap.geocoder({
      address: '常州市火车站',
      complete: res => {
        console.log(res.result.location); //经纬度对象
        let key = '6FLBZ-4M5KU-3ORVK-B7WDQ-MUDIF-M4FC4'; //使用在腾讯位置服务申请的key
        let referer = '地摊'; //调用插件的app的名称
        let endPoint = JSON.stringify({ //终点
          'name': '火车站',
          'latitude': parseInt(res.result.location.lat),
          'longitude': parseInt(res.result.location.lng)
        });
        wx.navigateTo({
          url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
        });
      }

    });


  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        console.log(JSON.stringify(res))
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        }
        else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },

  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },

  getLocal: function (latitude, longitude) {
    let vm = this;
    var  qqmapsdk = new QQMapWX({
      key: '64CBZ-OA5K4-KH3UV-DMCQX-2P4MJ-VJFQD'
    });
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log("获取地址："+JSON.stringify(res.result.address));
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        vm.data.city=city;
        console.log("city:"+city)
 
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserLocation();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})